package com.qfedu.controller;

import com.qfedu.entity.Grade;
import com.qfedu.entity.GradeWithStu;
import com.qfedu.entity.ResponseData;
import com.qfedu.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class GradeController {
    @Autowired
    GradeService gradeService;
    @RequestMapping("/getGradeList")
    public ResponseData getGradeList(int page,int limit){
        ResponseData<List<Grade>> gradeList = gradeService.getGradeList(page, limit);

        return  gradeList;
    }
    @RequestMapping ("/deleteGradeById")
    public ResponseData deleteGradeById(int id){
        ResponseData responseData=new ResponseData<>();
        int i = gradeService.deleteGradeById(id);
        if(i>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }
    @RequestMapping("/updateGradeById")
    public ResponseData updateGradeById(Grade grade){

        ResponseData responseData = new ResponseData();
        int i = gradeService.updateGrade(grade);
        if(i>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }
    @RequestMapping("/addGrade")
    public ResponseData addGrade(Grade grade){
        ResponseData responseData = new ResponseData();
        int i = gradeService.insertGrade(grade);
        if (i>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else{
            responseData.setMsg(ResponseData.FAIL_MSG);
            responseData.setCode(ResponseData.FAIL_CODE);
        }

        return responseData;
    }
    @RequestMapping("/gradeNameWithStuNum")
    public ResponseData gradeNameWithStuNum(){
        GradeWithStu gradeNameWithStuNum = gradeService.getGradeNameWithStuNum();
        ResponseData<GradeWithStu> responseData = new ResponseData();
        responseData.setData(gradeNameWithStuNum);
        return responseData;
    }
    @RequestMapping("/selectGrade")
    public ResponseData selectGrade(){
        ResponseData responseData =new ResponseData();
        List list = gradeService.selectGrade();
        responseData.setData(list);
        responseData.setMsg("班级");
        return responseData;
    }

}
